package com.yunfeisoft.business.controller;

import com.yunfeisoft.business.model.ElderHospitalization;
import com.yunfeisoft.business.service.inter.ElderHospitalizationService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: ElderHospitalizationController
 * Description: 住院治疗情况从表Controller
 * Author: Jackie liu
 * Date: 2020-04-04
 */
@Controller
public class ElderHospitalizationController extends BaseController {

    @Autowired
    private ElderHospitalizationService elderHospitalizationService;

    /**
     * 添加住院治疗情况从表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderHospitalization/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(ElderHospitalization record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "elderHealthId", "参数错误");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        Map<String, String[]> map = request.getParameterMap();
        // 获取healthId
        String healthId = record.getElderHealthId();
        // 获取入院时间
        String[] hospitalHistory1Starts = map.get("hospitalHistory1Start");
        // 获取出院时间
        String[] hospitalHistory1Ends = map.get("hospitalHistory1End");
        // 获取医疗机构名称
        String[] hospitalHistory1Orgnames = map.get("hospitalHistory1Orgname");
        // 获取住院原因
        String[] hospitalHistory1Reasons = map.get("hospitalHistory1Reason");
        // 获取病案号
        String[] hospitalHistory1Numbers = map.get("hospitalHistory1Number");
        // 获取类型
        String[] types = map.get("type");
        List<ElderHospitalization> hospitalizationList = new ArrayList<>();
        if (!ObjectUtils.isEmpty(hospitalHistory1Starts)) {
            for (int i = 0; i < hospitalHistory1Starts.length; i++) {
                if (StringUtils.isBlank(hospitalHistory1Starts[i]) && StringUtils.isBlank(hospitalHistory1Ends[i])
                        && StringUtils.isBlank(hospitalHistory1Orgnames[i]) && StringUtils.isBlank(hospitalHistory1Reasons[i])
                        && StringUtils.isBlank(hospitalHistory1Numbers[i])) {
                    continue;
                }
                ElderHospitalization elderHospitalization = new ElderHospitalization();
                elderHospitalization.setElderHealthId(healthId);
                elderHospitalization.setHospitalHistory1Start(DateUtils.stringToDate(hospitalHistory1Starts[i]));
                elderHospitalization.setHospitalHistory1End(DateUtils.stringToDate(hospitalHistory1Ends[i]));
                elderHospitalization.setHospitalHistory1Number(hospitalHistory1Numbers[i]);
                elderHospitalization.setHospitalHistory1Orgname(hospitalHistory1Orgnames[i]);
                elderHospitalization.setHospitalHistory1Reason(hospitalHistory1Reasons[i]);
                elderHospitalization.setType(types[i]);
                hospitalizationList.add(elderHospitalization);
            }
        }
        elderHospitalizationService.saveHospitalization(hospitalizationList);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改住院治疗情况从表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderHospitalization/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(ElderHospitalization record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        elderHospitalizationService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询住院治疗情况从表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderHospitalization/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String elderHealthId = ServletRequestUtils.getStringParameter(request, "elderHealthId", null);
        if (StringUtils.isBlank(elderHealthId)) {
            return ResponseUtils.warn("参数错误");
        }
        List<ElderHospitalization> record = elderHospitalizationService.getByHealthId(elderHealthId);
        Map<String, Object> result = new HashMap<>();
        result.put("personHospitalizationList", record.stream().filter(hospitalization -> "1".equals(hospitalization.getType())).collect(Collectors.toList()));
        result.put("familyHospitalizationList", record.stream().filter(hospitalization -> "2".equals(hospitalization.getType())).collect(Collectors.toList()));
        return ResponseUtils.success(result);
    }

    /**
     * 分页查询住院治疗情况从表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderHospitalization/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        Page<ElderHospitalization> page = elderHospitalizationService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除住院治疗情况从表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/elderHospitalization/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        elderHospitalizationService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
